package com.koron.order.linepatrol.nopatrol.controller;


import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.koron.bean.base.Response;
import com.koron.bean.query.PageQuery;
import com.koron.bean.workflow.RedirectQuery;
import com.koron.bean.workflow.WorkflowController;
import com.koron.common.core.business.workflow.templates.WorkFlowHandler;
import com.koron.common.core.config.ThreadLocalContext;
import com.koron.order.linepatrol.nopatrol.bean.dto.LineNoPatrolDTO;
import com.koron.order.linepatrol.nopatrol.bean.query.LineNoPatrolQuery;
import com.koron.order.linepatrol.nopatrol.bean.vo.LineNoPatrolVO;
import com.koron.order.linepatrol.nopatrol.service.LineNoPatrolService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.koron.ebs.mybatis.ADOConnection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 不巡视单(LineNoPatrol)表控制层
 *
 * @author jzp
 * @since 2022-11-30 14:14:12
 */
@Api(tags = "线路不巡视单接口")
@RestController
@RequestMapping("lineNoPatrol")
public class LineNoPatrolController implements WorkflowController {
    /**
     * 服务对象
     */
    @Autowired
    private LineNoPatrolService lineNoPatrolService;
    @Autowired
    private WorkFlowHandler workFlowHandler;

    private String getCurrDs() {
        return ThreadLocalContext.get().getCurrDs();
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ApiOperation(value = "根据id查询")
    @GetMapping("detail")
    public Response<LineNoPatrolDTO> selectOne(@RequestParam String id) {
        return Response.ok(ADOConnection.runTask(getCurrDs(), lineNoPatrolService, "getDtoById", LineNoPatrolDTO.class, id));
    }

    @ApiOperation(value = "分页查询")
    @GetMapping("page")
    public Response<PageInfo<LineNoPatrolVO>> page(PageQuery pageQuery, LineNoPatrolQuery query) {
        return Response.ok(ADOConnection.runTask(getCurrDs(), lineNoPatrolService, "page", PageInfo.class, pageQuery, query));
    }

    @ApiOperation(value = "保存或更新")
    @PostMapping("saveOrUpdate")
    public Response<LineNoPatrolDTO> saveOrUpdate(@RequestBody LineNoPatrolDTO lineNoPatrolDTO) {
        ADOConnection.runTask(getCurrDs(), lineNoPatrolService, "saveDto", Boolean.class, lineNoPatrolDTO);
        return Response.ok(lineNoPatrolDTO);
    }

    @ApiOperation(value = "提交")
    @PostMapping("submit")
    public Response<LineNoPatrolDTO> submit(@RequestBody LineNoPatrolDTO lineNoPatrolDTO) {
        ADOConnection.runTask(getCurrDs(), lineNoPatrolService, "submit", Boolean.class, lineNoPatrolDTO);
        return Response.ok(lineNoPatrolDTO);
    }

    @ApiOperation(value = "删除数据")
    @DeleteMapping("delete")
    public Response<String> delete(@RequestParam String id) {
        ADOConnection.runTask(getCurrDs(), lineNoPatrolService, "deleteById", Boolean.class, id);
        workFlowHandler.delete(id);
        return Response.ok();
    }

    /**
     * 工作流
     */
    @Override
    public Response<Object> updateState(@RequestBody RedirectQuery redirectQuery) {
        LineNoPatrolDTO lineNoPatrolDTO = JSON.parseObject(redirectQuery.getFormVariables(), LineNoPatrolDTO.class);
        // 提交
        if (redirectQuery.getOperation() == 32) {
            // 如果是结束
            if (redirectQuery.getCurNodeCode().equals("END")) {
                ADOConnection.runTask(getCurrDs(), lineNoPatrolService, "end", Boolean.class, lineNoPatrolDTO);
            } else {
                // 否则为提交
                ADOConnection.runTask(getCurrDs(), lineNoPatrolService, "submit", Boolean.class, lineNoPatrolDTO);
            }
        } else if (redirectQuery.getOperation() == 1 || redirectQuery.getOperation() == 256) {
            // 如果是驳回或撤回，回到草稿状态
            lineNoPatrolDTO.setStatus("0");
            ADOConnection.runTask(getCurrDs(), lineNoPatrolService, "saveDto", Boolean.class, lineNoPatrolDTO);
        }
        return Response.success();
    }

}

